package com.bsoft.cardviewtest;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

/**
 * Created by 泅渡者
 * Created on 2017/10/16.
 */

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {

    private Context mcontext;
    private List<Product> datas;

    public TestAdapter(Context mcontext, List<Product> datas) {
        this.mcontext = mcontext;
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_card, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.iv_image.getLayoutParams().height = getRandem();
        holder.iv_image.setImageResource(datas.get(position).getmImg());
        holder.tv_title.setText(datas.get(position).getmTitle());
        holder.card_view.setRadius(8);//设置图片圆角的半径大小
        holder.card_view.setCardElevation(8);//设置阴影部分大小
        holder.card_view.setContentPadding(5, 5, 5, 5);//设置图片距离阴影大小
    }

    private int getRandem() {
        Random random = new Random();
        int randNum = random.nextInt(100) + 101;
        return randNum;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_image;
        TextView tv_title;
        CardView card_view;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_image = (ImageView) itemView.findViewById(R.id.masonry_item_img);
            tv_title = (TextView) itemView.findViewById(R.id.masonry_item_title);
            card_view = (CardView) itemView.findViewById(R.id.card_view);
        }
    }

}
